package com.eleadmin.damon.biz.impl;


import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.utils.DateUtil;
import com.eleadmin.damon.biz.SequenceBiz;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @Author: lm
 * @Date: 2019/7/24
 **/
@Repository
public class SequenceBizImpl extends BaseDao implements SequenceBiz {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public String getSequence(String type, String companyId) {
        String sequence = "";

        String sql = "select * from sys_sequence where sequence_type = ? and companyId=? ";
        List rows =jdbcTemplate.queryForList(sql, new Object[] {type,companyId});
        if(!(rows !=null && rows.size()>0)){
            Map<String, String> map  = new HashMap<String, String>();
            String sql2 = "insert into sys_sequence (sequence_value,sequence_time," +
                    "sequence_type,sub_type,sequence_desc,sequence_width,companyId) " +
                    " values (?,?,?,?,?,?,?)";
            map.put("type",type);
            getMapParameter(new StringBuffer(sql2), map);
            jdbcTemplate.update(sql2, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setString(1, "0001");
                    pstmt.setString(2, DateUtil.getCurrentDate());
                    pstmt.setString(3, type);
                    pstmt.setString(4, "001");
                    pstmt.setString(5, "001");
                    pstmt.setString(6, "4");
                    pstmt.setString(7, companyId);
                }
            });
            return getSequence(type, companyId);
        }
        Iterator it = rows.iterator();
        if (it.hasNext()){
            Map userMap = (Map) it.next();
         //   SysSequence sysSequence = new SysSequence();
            long maxSequence = Long.parseLong((String) userMap.get("sequence_value"));

            int seqWidth =  Integer.parseInt((String)userMap.get("sequence_width")) ;
            Timestamp seqTime = Timestamp.valueOf((userMap.get("sequence_time").toString()).substring(0,19));
            long seqDay = seqTime.getTime() / (1000 * 3600 * 24);
            long nowDay = new Date().getTime() / (1000 * 3600 * 24);
            if (nowDay == seqDay)
                maxSequence++;
            else
                maxSequence = 1;
            sequence += maxSequence;

            int curWidth = sequence.length();
            for (int i = 0; i < seqWidth - curWidth; i++) {// 流水号补位
                sequence = "0" + sequence;
            }
            final String sequences = sequence;
            jdbcTemplate.update("update sys_sequence set sequence_value = ?,sequence_time = ?  where sequence_type = ? and companyId=? ", new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setString(1, sequences);
                    pstmt.setString(2, DateUtil.getCurrentDate());
                    pstmt.setString(3, type);
                    pstmt.setString(4, companyId);
                }
            });
        }
        return sequence;
    }
}
